package game.data;

import game.worlds.GameWorldOption;
import game.worlds.GameRole;
import game.data.ABProjectData.ABRoleAttrData;

using Reflect;

/**
 * 角色数据
 */
class RoleData {
	/**
	 * 移动速度，该值越大移动速度就越快
	 */
	public var speed:Float = 3.15;

	/**
	 * 跳跃力，该值越大跳跃的高度就越高
	 */
	public var jump:Float = 7.8;

	/**
	 * 最大的跳跃次数
	 */
	public var maxJumpTimes:Int = 2;

	/**
	 * 最大的允许空中攻击次数
	 */
	public var maxAirAttackTimes:Int = 1;

	/**
	 * 最大的允许残影次数
	 */
	public var maxForceSkillTimes:Int = 1;

	/**
	 * 连击数
	 */
	public var hitCounts:Int = 0;

	/**
	 * 击中伤害累积，会随着hitCounts一起清零
	 */
	public var hitDamage:Int = 0;

	/**
	 * 当前技能MP
	 */
	public var mp:Int = 0;

	/**
	 * 最大技能MP
	 */
	public var maxMp:Int = 0;

	/**
	 * 当前角色HP
	 */
	public var hp:Int = 0;

	/**
	 * 最大角色HP
	 */
	public var maxHp:Int = 0;

	/**
	 * 角色气力值
	 */
	public var effort:Int = 0;

	/**
	 * 最大角色气力值
	 */
	public var maxEffort:Int = 0;

	/**
	 * 物理攻击力
	 */
	public var attack:Int = 0;

	/**
	 * 魔法攻击力
	 */
	public var magic:Int = 0;

	/**
	 * 物理防御
	 */
	public var attackDefense:Int = 0;

	/**
	 * 魔法防御
	 */
	public var magicDefense:Int = 0;

	/**
	 * 物理防御力百分比加成
	 */
	public var attackDefenseScale:Float = 1;

	/**
	 * 魔法防御力百分比加成
	 */
	public var magicDefenseScale:Float = 1;

	/**
	 * 物理攻击力百分比加成
	 */
	public var attackScale:Float = 1;

	/**
	 * 魔法攻击力百分比加成
	 */
	public var magicScale:Float = 1;

	/**
	 * 移动速度百分比加成
	 */
	public var moveSpeedScale:Float = 1;

	/**
	 * 全局的攻击特效ID
	 */
	public var hitEffectId:String = null;

	/**
	 * 全局的重击特效ID
	 */
	public var hitBlowEffectId:String = null;

	/**
	 * 脚本ID
	 */
	public var script:String = null;

	public function new() {}

	/**
	 * 应用扩展数据
	 * @param data 
	 */
	public function applyData(data:ABRoleAttrData, option:GameWorldOption):Void {
		if (data == null)
			return;
		this.script = (data.script == null || data.script == "") ? "Script" : data.script;
		this.speed = data.speed;
		this.jump = data.jump;
		this.maxHp = Std.int(data.hp * (option.hpScale ?? 1) * 20000);
		this.attack = Std.int(data.attack * 100);
		this.magic = Std.int(data.magic * 100);
		this.attackDefense = 100;
		this.magicDefense = 100;
		this.maxEffort = Std.int(100 * (option.effortScale ?? 1));
		this.effort = this.maxEffort;
		this.hp = this.maxHp;
		this.maxMp = Std.int(data.energy) * 100;
		this.hitEffectId = data.hitEffectId;
		this.hitBlowEffectId = data.hitBlowEffectId;
	}

	/**
	 * 添加MP
	 * @param mp 
	 */
	public function addMp(mp:Int):Void {
		this.mp += mp;
		if (this.mp >= this.maxMp)
			this.mp = this.maxMp;
	}

	/**
	 * 计算攻击力，公式：伤害值 = 基础攻击力 * 伤害加成比
	 * @param role 
	 * @param hitData 
	 * @return Int
	 */
	public function mathAttack(hitData:HitData):Int {
		var attack:Int = 0;
		switch hitData.attackType {
			case NORMAL:
				attack = Std.int(this.attack * this.attackScale * hitData.damageScale);
			case MAGIC:
				attack = Std.int(this.magic * this.magicScale * hitData.damageScale);
			case REAL:
				if (this.attack > this.magic)
					attack = Std.int(this.attack * this.attackScale * hitData.damageScale);
				else
					attack = Std.int(this.magic * this.magicScale * hitData.damageScale);
		};
		return attack;
	}

	/**
	 * 计算防御力抵消后的伤害
	 * @param hitData 
	 * @return Int
	 */
	public function mathDefense(hurtData:HurtData):Int {
		var hurt:Int = hurtData.value;
		switch hurtData.attackType {
			case NORMAL:
				// 物理攻击
				var defense:Int = Std.int(this.attackDefense * this.attackDefenseScale);
				hurt = Std.int(hurt * (100 / defense));
			case MAGIC:
				// 魔法攻击
				var defense:Int = Std.int(this.magicDefense * this.magicDefenseScale);
				hurt = Std.int(hurt * (100 / defense));
			case REAL:
				// 如果是真实伤害，则直接忽略防御
		}
		return Math.round(Math.max(1, hurt));
	}
}
